define([
    "jquery",
    "IO",
    "bootbox",
    "bootstrap",
    "Multiselect",
],function($,IO,bootbox,bootstrap){
    /**---------------------------------初始化下拉菜单--------------------------------*/
    $(document).ready(function(){
        //初始化下拉菜单
        $('#example-getting-started-teacher').multiselect({
            enableFiltering: true,
            includeSelectAllOption: true,
            nonSelectedText:"请选择老师用户",
            dropDown: true,
            onChange: function(option, checked, select) {

            }
        });
        $('#example-getting-started-leader').multiselect({
            enableFiltering: true,
            includeSelectAllOption: true,
            nonSelectedText:"请选择企业领导",
            dropDown: true,
            onChange: function(option, checked, select) {
                // alert( $(option).val() + '.');
                // alert( checked);
            }
        });
        $('#example-getting-started-normal').multiselect({
            enableFiltering: true,
            includeSelectAllOption: true,
            nonSelectedText:"请选择普通成员",
            dropDown: true,
            onChange: function(option, checked, select) {

            }
        });
    })
    /**---------------------------------加载主体内容--------------------------------*/
    $(function () {
        getTable();
        function getTable(){
            var grid;
            //每列数据列性
            var columns = [
                {
                    title: '用户类型',
                    dataIndex: 'name',
                    dataSource: function (data, datarow, gridobj, current_column) {
                        var user = data[datarow].user_type;
                        if(user == 0){
                            return "普通用户";
                        }else if(user==1){
                            return "教师用户";
                        }else{
                            return "企业领导";
                        }
                    }
                },
                {
                    title: '姓名',
                    dataIndex: 'gender',
                    dataSource: function (data, datarow, gridobj, current_column) {
                        return "<span>"+ data[datarow].user_name+"</span>";
                    }
                },
                {
                    title: '是否是组长',
                    dataIndex: 'gender',
                    dataSource: function (data, datarow, gridobj, current_column) {
                        var is_leader = data[datarow].is_leader;
                        if(is_leader == 0){
                            return "否";
                        }else{
                            return "<span style='color: red;font-size: 20px'>"+"是"+"</span>";
                        }
                    }
                },
                {
                    title: '操作',
                    dataIndex: 'action',
                    dataSource: function (data, datarow, gridobj, current_column) {
                        return "<span><a href='javascript:void(0)' style='color: red;' class='delete' user_id='"+data[datarow].user_id+"'>删除</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='javascript:void(0)' style='color: dodgerblue;' class='toBeTop' user_id='"+data[datarow].user_id+"'>设为组长</a></span>";
                    }
                },

            ];


            //数组初始化
            function dataInit() {
                grid = new Grid("/ct/act/addMumber", $("#table_my_toast"), $("#paging_my_toast"), 1, 10, columns, get_input_param(), 100);

                //初始化
                grid.Init();
            }
            //获取输入框内容
            function get_input_param() {
                var team_id = $("#team_id_hidden").attr("value");
                var params = "team_id="+team_id;
                return params;
            }
            dataInit();
        }

        /**---------------------------------点击"新增成员"--------------------------------*/
        $("#addConsultingclassification").on('click',function(){
            $("#myModal").modal('show');
        });
        /**---------------------------------点击模态框中的增加按钮--------------------------------*/
        $("#add").on("click",function(){
            var user_idString_all = [];
            var user_idString0 = [];
            var user_idString1 = $("#example-getting-started-teacher").val();
            var user_idString2 = $("#example-getting-started-leader").val();
            var user_idString3 = $("#example-getting-started-normal").val();
            if(user_idString1!=null) {
                user_idString_all = user_idString1.concat(user_idString2).concat(user_idString3);
            }else if(user_idString1==null&&user_idString2!=null){
                user_idString_all = user_idString2.concat(user_idString3);
            }else if(user_idString1==null&&user_idString2==null){
                user_idString_all = user_idString3;
            }else if(user_idString1==null&&user_idString2==null&&user_idString3==null){
                bootbox.alert("请选择用户");
                return;
            }else{
                bootbox.alert("我也不知道哪里错了!");
                return;
            }
            if(user_idString_all!=null) {
                for (var i = 0; i < user_idString_all.length; i++) {
                    if (user_idString_all[i] != null) {
                        user_idString0.push(user_idString_all[i]);
                    }
                }
            }else{
                bootbox.alert("请选择用户");
                return;
            }
            var user_idString = user_idString0.toString();

            var params={};
            var team_id = $("#team_id_hidden").attr("value");
            params.team_id = team_id;
            params.user_idString = user_idString
            IO.post("/ct/act/mumber_add",params, function (d) {
                if (d.code == 0) {
                    $("#myModal").modal('hide');
                    bootbox.alert(d.msg);
                    getTable();
                }

            });
        });
        /**---------------------------------删除操作--------------------------------*/
        $(document).on("click",".delete",function(){
           var user_id=$(this).attr("user_id");
           var team_id = $("#team_id_hidden").attr("value");
            if(user_id){
                bootbox.confirm("确定要删除该分类？",function(result){
                    if(result) {
                        IO.get("/ct/act/mumber_delete",{user_id:user_id,team_id:team_id},function(d){
                            if(d.code == 0) {
                                bootbox.alert(d.msg);
                                getTable();
                            }
                        });
                    }
                });
            }
        });
        /**---------------------------------设置组长--------------------------------*/
        $(document).on("click",".toBeTop",function(){
            var user_id=$(this).attr("user_id");
            var team_id = $("#team_id_hidden").attr("value");
            if(user_id){
                bootbox.confirm("确定设置该成员为组长？",function(result){
                    if(result) {
                        IO.get("/ct/act/set_leader",{user_id:user_id,team_id:team_id},function(d){
                            if(d.code == 0) {
                                bootbox.alert(d.msg);
                                getTable();
                            }
                        });
                    }
                });
            }
        });
    });

})
